﻿using AppData;
using System.Collections.Generic;

public class users : SqlTableClass
{
    public string password = "";
    public string nickname;
    public int max_lv;
    public int coin;
    public int diamond;
    public int home_ad_remain;
    public double last_login_time;
    public double last_offline_time;
    public byte is_robot;
    public int login_day;
    public string obtain_login_bonus_day;
    public int obtain_shared_reward;
    public string friends;
    public string myfollows;
    public string followers;

    public byte gender;//true: Man, false: woman
    public string name;
    public int birth_date;
    public string location_province;
    public string location_city;
    public string about_me;
   
    public users():base()
    {

    }


    public static users DefaultValue(bool is_robot)
    {
        users user = new users();
        user.nickname = "GUEST";
        user.max_lv = 1;
        user.coin = 0;
        user.diamond = 0;
        user.home_ad_remain = 0;
        //user.last_login_time = last_login_time;
        //user.last_offline_time = last_offline_time;
        user.is_robot = 0;
        user.login_day = 0;
        user.obtain_login_bonus_day = ObtainedLoginBonusDay2String(new List<int> { 1 });
        user.obtain_shared_reward = 0;
        user.gender = 0;
        user.name = "GUEST";
        user.followers = "";

        return user;
    }

    public static long DeviceId2UserTableId(long id)
    {
        if (id < 0)
        {
            return id;
        }

        long index = (id / ServerConfig.MaxDBUserID);
        return index + 1;
    }
    public static long UserTableId2DeviceId(string databaseName, long userId)
    {
        string dbIdStr = databaseName.Replace(ServerConfig.DBUserBase, "");
        int.TryParse(dbIdStr, out int dbId);
        return UserTableId2DeviceId(dbId, userId);
    }
    public static long UserTableId2DeviceId(int dbId, long userId)
    {
        if (dbId > 0 && dbId <= ServerConfig.MaxDBUserID)
        {
            return (dbId - 1) + (userId - 1) * ServerConfig.MaxDBUserID;
        }
        else
        {
            GameDebug.LogError($"invalid user dbId:{dbId}");
            return -1;
        }

    }

    public static string GetUserDatabaseWithDeviceId(long id)
    {
        //if (id < 0)
        //{
        //    return ServerConfig.DBUserRobot;
        //}

        int dbId = (int)(id % 10) + 1;
        return GetUserDatabaseWithDbId(dbId);
    }
    public static string GetUserDatabaseWithDbId(int dbId)
    {
        string databaseName = $"{ServerConfig.DBUserBase}{dbId}";

        return databaseName;
    }

    public static List<long> String2LongList(string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            return new List<long>();
        }
        List<long> longs = new List<long>();
        var res = str.Split('|');
        for (int i = 0; i < res.Length; i++)
        {
            long.TryParse(res[i].Trim(), out long result);
            longs.Add(result);
        }
        return longs;
    }

    public static string LongList2String(List<long> list)
    {
        string res = "";
        for (int i = 0; i < list.Count; i++)
        {
            if (i < list.Count - 1)
                res += list[i] + "|";
            else
                res += list[i];
        }
        return res;
    }

    #region UpdateValues
    public static int UpdateDiamond(long deviceId, int diamond)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        return MysqlFunction.UpdateValue(database,SqlTable.users.ToString(), "diamond", diamond.ToString(), "id", userTableId.ToString());
    }
    public static int UpdateLastLoginTime(long deviceId, double lastLoginTime)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        return MysqlFunction.UpdateValue(database,SqlTable.users.ToString(), "last_login_time", lastLoginTime.ToString(), "id", userTableId.ToString());
    }
    public static int UpdateLastOfflineTime(long deviceId, double lastOfflineTime)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        return MysqlFunction.UpdateValue(database,SqlTable.users.ToString(), "last_Offline_time", lastOfflineTime.ToString(),  "id", userTableId.ToString());
    }
    public static int UpdateChargeTime(long deviceId, int chargeTimes)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        return MysqlFunction.UpdateValue(database,SqlTable.users.ToString(), "charge_count", chargeTimes.ToString(), "id", userTableId.ToString());
    }
    public static int UpdateHomeAdTimesRemain(long deviceId, int remain)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        return MysqlFunction.UpdateValue(database,SqlTable.users.ToString(), "home_ad_remain", remain.ToString(), "id", userTableId.ToString());
    }
    public static int UpdateNick(long deviceId, string nick)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        return MysqlFunction.UpdateValue(database,SqlTable.users.ToString(), "nickname", nick, "id", userTableId.ToString());
    }
    public static int UpdateMaxLv(long deviceId, int newLv)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        return MysqlFunction.UpdateValue(database, SqlTable.users.ToString(), "max_lv", newLv.ToString(), "id", userTableId.ToString());
    }

    public static int UpdateLoginDay(long deviceId, int loginDay)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        return MysqlFunction.UpdateValue(database, SqlTable.users.ToString(), "login_day", loginDay.ToString(), "id", userTableId.ToString());
    }

    public static List<int> ObtainedLoginBonusString2List(string str)
    {
        if (str == null)
        {
            return new List<int>();
        }
        List<int> res = new List<int>();
        for (int i = 0; i < str.Length; i++)
        {
            if (str[i].Equals('1'))
            {
                res.Add(i + 1);
            }
        }
        return res;
    }

    const int maxObtainLoginBonusDay = 7;
    static string ObtainedLoginBonusDay2String(List<int> obtainedDay)
    {
        string obtainLoginBonusDay = "";
        for (int i = 1; i <= maxObtainLoginBonusDay; i++)
        {
            if (obtainedDay.Contains(i))
            {
                obtainLoginBonusDay += "1";
            }
            else
            {
                obtainLoginBonusDay += "0";
            }
        }
        return obtainLoginBonusDay;
    }

    public static int UpdateObtainedLoginBonus(long deviceId,  List<int> obtainedDay)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        return MysqlFunction.UpdateValue(database, SqlTable.users.ToString(), "obtain_login_bonus_day", ObtainedLoginBonusDay2String(obtainedDay), "id", userTableId.ToString());
    }

    public static int UpdateObtainSharedReward(long deviceId, int value)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        return MysqlFunction.UpdateValue(database, SqlTable.users.ToString(), "obtain_shared_reward", value.ToString(), "id", userTableId.ToString());
    }

    public static int UpdateBaseInfo(long deviceId, int gender, int birthYear, int education, string locationProv,string locationCity)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        string[] keys = new string[] { "gender", "birthYear", "education", "location_province","location_city" };

        string[] values = new string[] { gender.ToString(),birthYear.ToString(), education.ToString(), locationProv, locationCity };

        return MysqlFunction.UpdateValues(database, SqlTable.users.ToString(), keys, values, new string[] { "id" }, new string[] { userTableId.ToString() });
    }
    
    public static int UpdateMyFollows(long deviceId, List<long> follows)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        string listStr = LongList2String(follows);
        return MysqlFunction.UpdateValue(database, SqlTable.users.ToString(), "myfollows", listStr, "id", userTableId.ToString());
    }
    public static int UpdateFollowers(long deviceId, List<long> followers)
    {
        string database = users.GetUserDatabaseWithDeviceId(deviceId);
        long userTableId = users.DeviceId2UserTableId(deviceId);

        string followersStr = LongList2String(followers);
        return MysqlFunction.UpdateValue(database, SqlTable.users.ToString(), "followers", followersStr, "id", userTableId.ToString());
    }
    #endregion
}